from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings


def generic_open_id(openid):
    #创建实例对象， secret_key  秘钥， expires_in  数据过期时间，秒数
    s = Serializer(secret_key=settings.SECRET_KEY,expires_in=3600)

    # 3，组织数据，加密
    data = {
    'openid':openid
}

    secret_data = s.dumps(data)

    #bytes转   str
    return secret_data.decode()

###################################解密
from  itsdangerous import BadSignature

def check_open_id(token):

    s = Serializer(secret_key=settings.SECRET_KEY,expires_in=3600)

    try:
        data = s.loads(token)
    except BadSignature:
        return None

    return data.get('openid')